$(function() {
	question_list.initial();
});

function getFocus(obj){
	$("#question-filter-field dd span").each(function(){
		//console.log($(this));
		if($(this).hasClass("label")) {
			if($(this).data("id") != "0" && $(this).data("id") != 0){
				modal.showProgress();
				question_list.getField2Point($(this).data("id"));
			} else {
				util.error("请先选择题库类型！");
				return;
			}
		}
	});
	if(obj.value != '' && obj.value.length > 0){
		obj.value='';
		obj.style.color='#606060';
	}
}
    
function hide_div(e){
	var biaoqian = "click";
	var classname= $(e.target)[0].className;
	if(classname.indexOf('click')>'-1' ||$(e.target)[0].id=='point_name')
		return ;
	if($('#point_name').val()=='')
    {
    	$('#point_name').val('请输入知识点名称/拼音/首字母搜索');
    	$('#point_name').css('color','#B7B7B7');
    }
    $("#pointarea").hide();
} 

function pop_click(){
	$("#pointarea").hide();
}

function check_point_get(){
	var field = $("#field-select > option:selected");
	if(field.length == 0) {
		util.error("请先选择专业分类列表");
	}
}

function append_city(pointarea){
	$("#citylist").empty();
	//$("#citylist").append("");
	$("#citylist").append(pointarea+"");
	$('#pointarea').show();
}

//直接输入地址框的onblur事件
function input_blur()
{
	var value = $('#point_name').val();
	if($.trim(value)!='请输入知识点名称/拼音/首字母搜索' && $('#pointarea').css('display')=='none')
	{
		$('#point_name').val('请输入知识点名称/拼音/首字母搜索');
    	$('#point_name').css('color','#B7B7B7');
	}	
}
    
//直接输入地址框的onkeyup事件
function input_keyup()
{
	var value = $('#point_name').val();
	if($('#hid_real_point_name').val()!=value || $('#hid_real_point_name').val()=='')
	{
		$('#pointarea').hide();
		
	}else if(value==$('#hid_real_point_name').val())
	{
		$('#pointarea').hide();
	}
	if($.trim(value) == '') {
		$('#pointarea').show();
	}
		
}

function check_input(){
	var value = $('#point_name').val();
	if(value==$('#hid_real_point_name').val() && $("#hid_point_name").val()!=""){
		return true;
	}
	return false;
}

/**
 * 字母页面内链
 *
 */
function letter_scroll(letter)
{
	 var obj = $("#c_"+letter);
	 $('html,body').animate({scrollTop: obj.offset().top}, 500);
}

/**
 * 用户点击产品后，产品名称填入input框
 *
 */
function change_city_val(pointname, pointid)
{
	//加载数据
	var genrateParamOld = question_list.genrateParamOld();
	genrateParamOld.knowledge = pointid;
	question_list.redirectUrl(genrateParamOld);
}

function selectAll(){
	$("#point-from-select option").each(function() { 
		$(this).attr("selected", "selected"); 
	});
}

function check(name){
	var flag = true;
	$("#point-from-select option").each(function (){  
	    if($(this).text()==name){   
	    	alert("您已经选择过该知识点，请重新选择");
	    	flag = false;
	 	}
	});
	return flag;
}

function tabCutover(c,d){$(c).parent().attr("class");$(c).parent().children().removeClass("current");$(c).addClass("current");$("."+d).parent().children().hide();$("."+d).show();}

var modal = {
	prepare : function prepare() {
		$(".content").append("<div id=\"loading-progress\" style=\"display:none;\"><div id=\"loading-content\"> <h2>数据加载中</h2><img class=\"loading-gif\" src=\"resources/images/loading.gif\"/><div> </div>");

	},
	showProgress : function showProgress() {
		$("#loading-progress").show();
	},

	hideProgress : function hideProgress() {
		$("#loading-progress").hide();
	}
};

var question_list = {
	initial : function initial() {
		this.bindChangeSearchParam();
	},
	
	bindChangeSearchParam : function bindChangeSearchParam(){
		$("#question-filter dl dd span").click(function(){
			if($(this).hasClass("label"))return false;
			var genrateParamOld = question_list.genrateParamOld();
			if($(this).parent().parent().attr("id") == "question-filter-field" ){
				genrateParamOld.field = $(this).data("id");
				question_list.redirectUrl(genrateParamOld);
			}else if($(this).parent().parent().attr("id") == "question-filter-knowledge" ){
				genrateParamOld.knowledge = $(this).data("id");
				question_list.redirectUrl(genrateParamOld);
			}else{
				genrateParamOld.questiontype = $(this).data("id");
				question_list.redirectUrl(genrateParamOld);
			}
		});
		$(".pagination li a").click(function(){
			var pageId = $(this).data("id");
			if(pageId==null||pageId=="")return false;
			var genrateParamOld = question_list.genrateParamOld();
			genrateParamOld.page = pageId;
			question_list.redirectUrl(genrateParamOld);
			
		});
	},
	
	genrateParamOld :function genrateParamOld(){
		var field = $("#question-filter-field dd .label").data("id");
		var knowledge = $("#question-filter-knowledge dd .label").data("id");
		var questiontype = $("#question-filter-qt dd .label").data("id");
		var searchParam = 0;
		var page = 1;
		
		var data = new Object();
		data.field = field;
		data.knowledge = knowledge==null?0:knowledge;
		data.questiontype= questiontype;
		data.searchParam = searchParam;
		data.page = page;
		return data;
	},

	redirectUrl : function(newparam) {
		var paramurl = newparam.field;
		paramurl = paramurl + "-" + newparam.knowledge;
		paramurl = paramurl + "-" + newparam.questiontype;
		paramurl = paramurl + "-" + newparam.searchParam;
		paramurl = paramurl + "-" + newparam.page;
		paramurl = paramurl + ".html";

		document.location.href = document.getElementsByTagName('base')[0].href
				+ 'admin/questionfilter-' + paramurl;
	},
	
	getField2Point : function(field) {
		$.ajax({
			headers : {
				'Accept' : 'application/json',
				'Content-Type' : 'application/json'
			},
			type : "GET",
			url : "admin/get-knowledge-point-2/" + field,
			success : function(message,tst,jqXHR) {
				if(!util.checkSessionOut(jqXHR)) return false;
				if (message.result == "success") {
					//point_list.empty();
					var pointarea = message.object.html;
					var data = message.object.data;
					$('#point_name').autocomplete(data, {
						max: 12, //列表里的条目数
						minChars: 0, //自动完成激活之前填入的最小字符
						width: 525, //提示的宽度，溢出隐藏
						scrollHeight: 500, //提示的高度，溢出显示滚动条
						matchContains: true, //包含匹配，就是match字段值里面的数据，是否只要包含文本框里的数据就显示
						autoFill: false, //自动填充
						multiple: true,
						minChars:1,
						formatItem: function(row, i, max) {
							return row.pointname;
						},
						formatMatch: function(row, i, max) {
							return row.match;
						},
						formatResult: function(row) {
							return row.pointname;
						},resultsClass:'ac_results'
					}).result(function(event, row, formatted) {
						if(row.pointid != '' && row.pointid != null && row.pointname != '' && row.pointname != null){
							//加载数据
							var genrateParamOld = question_list.genrateParamOld();
							genrateParamOld.knowledge = row.pointid;
							question_list.redirectUrl(genrateParamOld);
						}
					});
					modal.hideProgress();
					append_city(pointarea);
				} else {
					util.error("操作失败请稍后尝试");
				}
			},
			error : function(xhr) {
				util.error("操作失败请稍后尝试");
			}
		});
	}
};